<template>
  <div class="app-container">
    <div v-if="orderId === '-1'">
      <el-tag effect="dark">全部订单</el-tag>
    </div>
    <div v-else>
      <el-tag effect="dark">模块:{{ name }}-客户地址:{{ customAddr }}</el-tag>
    </div>
    <el-upload
      ref="upload"
      :action="uploadFileFormAction"
      :headers="headers"
      list-type="picture-card"
      :file-list="uploadFormfileList"
      :on-preview="handlePictureCardPreview"
      :before-remove="beforeRemove"
      :on-remove="handleRemove"
      :on-error="handleError"
      :before-upload="handleBeforeUpload"
    >
      <i class="el-icon-plus" />
      <div slot="file" slot-scope="{file}">
        <img class="el-upload-list__item-thumbnail" :src="file.url" alt="">
        <span class="el-upload-list__item-actions">
          <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
            <i class="el-icon-zoom-in" />
          </span>
          <span class="el-upload-list__item-download" @click="handleDownload(file)">
            <i class="el-icon-download" />
          </span>
          <span class="el-upload-list__item-delete" @click="handleRemove(file)">
            <i class="el-icon-delete" />
          </span>
        </span>
      </div>
    </el-upload>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>
<script>
import {
  getImages,
  removeImageFile
} from '@/api/zxlib'
import { getToken } from '@/utils/auth' // get token from cookie
export default {
  data() {
    return {
      uploadFileFormAction: process.env.VUE_APP_BASE_API,
      headers: {
        Authorization: 'Bearer ' + getToken()
      },
      dialogImageUrl: '',
      dialogVisible: false,
      uploadFormfileList: [],
      name: '',
      module: '',
      orderId: '-1',
      customAddr: ''
    }
  },
  created() {
    if (this.$route.params && this.$route.params.orderId) {
      this.name = this.$route.params.name
      this.module = this.$route.params.module
      this.orderId = this.$route.params.orderId
      this.customAddr = this.$route.params.customAddr
      this.uploadFileFormAction = this.uploadFileFormAction + '/api/v1/zxlib/uploads/' + this.module
    }
    this.uploadFileFormAction = this.uploadFileFormAction + '/' + this.orderId
    getImages('zxdesign', this.orderId).then(response => {
      response.data.forEach(file => {
        this.uploadFormfileList.push({ fileUrl: file.url, name: file.name, url: process.env.VUE_APP_BASE_API + '/' + file.url })
      })
    })
  },
  methods: {
    handleBeforeUpload() {
      if (this.orderId === '-1') {
        this.msgError('未选择订单，只能预览, 不能上传')
        return false
      }
    },
    // 删除图片
    beforeRemove(file, fileList) {
      // if (this.orderId === '-1') {
      //   this.msgError('未选择订单，只能预览')
      //   return false
      // }
      // let a = true
      // if (file && file.status === 'success') {
      //   a = this.$confirm(`确定移除 ${file.name}？`)
      // }
      // return a
    },
    handleDownload(file) {
      var a = document.createElement('a')
      a.setAttribute('href', file.url)
      a.setAttribute('target', '_blank')
      a.setAttribute('id', 'camnpr')
      document.body.appendChild(a)
      a.click()
      document.body.removeChild(a)
    },
    handleRemove(file, fileList) {
      // {"name":"id2.jpg",
      // "url":"http://127.0.0.1:8000/uploads/zxbuild/7/id2.jpg",
      // "uid":1598709260240,
      // "status":"success"};
      if (file && file.status === 'success') {
        removeImageFile(file.fileUrl).then(resp => {
          if (resp.code === 200) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.uploadFormfileList.splice(this.uploadFormfileList.indexOf(file), 1)
            return true
          } else {
            this.msgError('删除失败')
          }
        })
      }
      return false
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    handleError(err, file, fileList) {
      if (this.orderId === undefined) {
        this.msgError('请先选择订单')
      } else {
        this.msgError(err)
      }
    }
  }
}
</script>
<style>
.el-upload__tip {
  line-height: 1.2;
}

</style>
